package com.sec.android.app.myfiles.module.preview;

import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.sec.android.app.myfiles.feature.detailinfo.DetailsInfoMgr;
import com.sec.android.app.myfiles.module.abstraction.AbsFileListAdapterImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo;
import com.sec.android.app.myfiles.widget.listview.AbsListViewImp;
import com.sec.android.app.myfiles.widget.listview.FileListViewHolder;
import com.sec.android.app.myfiles.widget.listview.ListViewHolder;

public class PreviewCompressAdapterImp extends AbsFileListAdapterImp {

    public PreviewCompressAdapterImp(Context context, NavigationInfo navigationInfo, AbsListViewImp listView, Cursor c, int flags) {
        super(context, navigationInfo, listView, c, flags);
    }

    @Override
    protected void _bindView(Context context, ListViewHolder vh, View v, FileRecord record, int position) {
        super._bindView(context, vh, v, record, position);
        DetailsInfoMgr.getInstance().loadChildCount(context, record, vh.getView(FileListViewHolder.SIZE, TextView.class));
    }

    @Override
    public String getSelection() {
        FileRecord record = mNavigationInfo.getCurRecord();
        if (record instanceof CompressFileRecord) {
            CompressFileRecord compressRecord = (CompressFileRecord) record;
            StringBuilder sb = new StringBuilder();
            DbTableInfo tableInfo = PreviewCompressDbTableInfo.getInstance();
            FileRecord archiveRecord = compressRecord.getArchiveRecord();
            sb.append(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.ARCHIVE_PATH)).append("=? AND ")
                    .append(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.ARCHIVE_ID)).append("='")
                    .append(String.valueOf(archiveRecord.getSize())).append("' AND ");
            String path = compressRecord.getFullPath();
            if (TextUtils.isEmpty(path)) {
                sb.append(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.PATH)).append(" IS NULL");
            } else {
                sb.append(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.PATH)).append("='").append(compressRecord.getFullPath()).append('\'');
            }
            return sb.toString();
        }
        return null;
    }

    @Override
    public String[] getSelectionArgs() {
        String archiveFilePath = null;
        FileRecord record = mNavigationInfo.getCurRecord();
        if (record instanceof CompressFileRecord) {
            CompressFileRecord compressRecord = (CompressFileRecord) record;
            FileRecord archiveRecord = compressRecord.getArchiveRecord();
            archiveFilePath = archiveRecord.getFullPath();
        }
        return new String[]{
                archiveFilePath};
    }

    @Override
    protected FileRecord _getFileRecord(Cursor cursor) {
        FileRecord ret = null;
        // final int archivePathIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.ARCHIVE_PATH);
        final int pathIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.PATH);
        final int nameIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.NAME);
        final int sizeIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.SIZE);
        final int dateIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.DATE);
        final int typeIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.FILE_TYPE);
        // final int hiddenIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.IS_HIDDEN);
        final int cntIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.ITEM_COUNT);
        final int cntHiddenIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.ITEM_COUNT_HIDDEN);

        if (cursor != null) {
            // String archivePath = cursor.getString(archivePathIndex);
            String path = cursor.getString(pathIndex);
            String name = cursor.getString(nameIndex);
            long size = cursor.getLong(sizeIndex);
            long date = cursor.getLong(dateIndex);
            int fileType = cursor.getInt(typeIndex);

            // int isHidden = cursor.getInt(hiddenIndex);
            int itemCount = cursor.getInt(cntIndex);
            int itemCountHidden = cursor.getInt(cntHiddenIndex);

            // ret = FileRecord.createFileRecord(getStorageType(), -1, path, name, size, date, fileType, isHidden, itemCount, itemCountHidden);

            FileRecord archiveFileRecord = ((CompressFileRecord) mCurRecord).getArchiveRecord();
            ret = new CompressFileRecord(archiveFileRecord, path, name, size, date, fileType, 0, itemCount, itemCountHidden);
        }
        return ret;
    }

    @Override
    public String getFilePath(Cursor cursor) {
        return getFilePath(cursor, mTableInfo.getIndex(DbTableInfo.COLUMN_ID.PATH), mTableInfo.getIndex(DbTableInfo.COLUMN_ID.NAME));
    }

    @Override
    public FileRecord.StorageType getStorageType() {
        return FileRecord.StorageType.Compress;
    }
}
